package ar.edu.unq.mongodbexample.tests.persitence;

import java.util.ArrayList;
import java.util.List;

import ar.edu.unq.mongodbexample.homes.CoursesHome;
import ar.edu.unq.mongodbexample.homes.ProfessorHome;
import ar.edu.unq.mongodbexample.homes.StudentHome;
import ar.edu.unq.mongodbexample.model.Course;
import ar.edu.unq.mongodbexample.model.Professor;
import ar.edu.unq.mongodbexample.model.Student;
import ar.edu.unq.mongodbexample.persistence.MongoDBManager;
import junit.framework.TestCase;

public class CoursesHomeTestCase extends TestCase {

	CoursesHome home;
	List<Course> courses;
	
	protected void setUp() throws Exception {
		this.home= new CoursesHome();
		this.courses=getCourses();
	}

	protected void tearDown() throws Exception {
		MongoDBManager.getInstance().getDataStore().getCollection(Course.class).drop();
		MongoDBManager.getInstance().getDataStore().getCollection(Student.class).drop();
		MongoDBManager.getInstance().getDataStore().getCollection(Professor.class).drop();
	}
	
	public void testSaveAndGetCourseById(){
		Course course=new Course(1,"CatedraPrueba");
		home.save(course);
		assertEquals("CatedraPrueba", home.findById(1).getName());
	}
	
	public void testSaveAndGetCourseByIdAndGetReferences(){
		ProfessorHome professorHome=new ProfessorHome();
		Professor professor=new Professor(12345678,"Tesone","Pablo","pablo.tesone@gmail.com");
		professorHome.save(professor);
		
		StudentHome studentHome=new StudentHome();
		List<Student> students=createStudents();
		studentHome.saveAll(students);
		
		Course course=new Course(1,"CatedraPrueba");
		course.setStudents(students);
		course.setProfessor(professor);
		home.save(course);
		
		Course courseFound=home.findById(1);
		assertEquals("CatedraPrueba", courseFound.getName());
		assertEquals(students.size(), courseFound.getStudents().size());
		assertEquals(professor.getDni(), courseFound.getProfessor().getDni());
	}
	
	public void testSaveAndGetCourseByName(){
		Course course=new Course(1,"CatedraPrueba");
		home.save(course);
		assertEquals(1, home.getByName("CatedraPrueba").getId());
	}
	
	public void testSaveAndGetCourseByNameWithNonExistNameShouldBeNull(){
		Course course=new Course(1,"CatedraPrueba");
		home.save(course);
		assertEquals(null, home.getByName("CatedraQueNoExiste"));
	}
	
	public void testSaveAllAndGetAllCourses(){
		home.saveAll(courses);
		List<Course> coursesList=home.getAll();
		assertEquals(4, coursesList.size());
	}
	
	
	private List<Course> getCourses(){
		List<Course> coursesList=new ArrayList<Course>();
		coursesList.add(new Course(1,"Catedra1"));
		coursesList.add(new Course(2,"Catedra2"));
		coursesList.add(new Course(3,"Catedra3"));
		coursesList.add(new Course(4,"Catedra4"));
		return coursesList;
	}
	
	private List<Student> createStudents() {
    	List<Student> studentsList=new ArrayList<Student>();
        studentsList.add(new Student(00000001,19523,"UnApellido1","UnNombre1","unemailmorphia1@mail.com"));
        studentsList.add(new Student(00000002,19524,"UnApellido2","UnNombre2","unemailmorphia2@mail.com"));
        studentsList.add(new Student(00000003,19525,"UnApellido3","UnNombre3","unemailmorphia3@mail.com"));
        studentsList.add(new Student(00000004,19526,"UnApellido4","UnNombre4","unemailmorphia4@mail.com"));
        return studentsList;
    }
}
